package com.tyqx.hashchainbiz.controller.test;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tyqx.hashchainbean.test.BaiWanTest;
import com.tyqx.hashchainbean.vo.ImageVO;
import com.tyqx.hashchainbean.vo.MongoDbInfoVO;
import com.tyqx.hashchainbiz.service.BaiWanTestService;
import com.tyqx.hashchainbiz.service.MysqlDataBaseAndMongDbConfigService;
import com.tyqx.hashchainbiz.service.impl.ExcelService;
import com.tyqx.hashchainutil.common.mongodb.ImagePathHashValueUtil;
import hashchainutil.common.BackendPage;
import hashchainutil.common.Dto;
import hashchainutil.common.DtoUtil;
import hashchainutil.common.EmptyUtils;
import org.hyperledger.fabric.sdk.security.CryptoPrimitives;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/baiwan")
@CrossOrigin
public class BaiWanTestController {

    @Resource
    private BaiWanTestService baiWanTestService;

    @Resource
    private MysqlDataBaseAndMongDbConfigService mysqlDataBaseAndMongDbConfigService;

    @Resource
    private ExcelService excelService;

    @Resource
    private MongoTemplate mongoTemplate;


    @RequestMapping(value = "/batchAddBaiWan", method = RequestMethod.POST)
    @ResponseBody
    public Dto batchAddBaiWan(@RequestParam("fileExcel")MultipartFile file, @RequestParam("attachment")MultipartFile attachment) throws Exception {

        if (file.isEmpty()) {
            return DtoUtil.returnFail("文件不能为空","100001");
        }
        if (attachment.isEmpty()) {
            return DtoUtil.returnFail("attachment不能为空","100002");
        }
        String msg="";
        try {
            InputStream inputStream = file.getInputStream();
            List<List<Object>> list = excelService.getBankListByExcel(inputStream, file.getOriginalFilename());
            inputStream.close();
            Integer flag=0;
            List<BaiWanTest> baiWanTestList=new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                List<Object> lo = list.get(i);
                BaiWanTest baiWanTest=new BaiWanTest();
                for (int j=0;j<lo.size();j++){
                    if(j==0){
                        baiWanTest.setDateTest(lo.get(j).toString());
                    }else if(j==1){
                        baiWanTest.setZhaiYao(lo.get(j).toString());
                    }else if(j==2){
                        baiWanTest.setJieKeMu(lo.get(j).toString());
                    }else if(j==3){
                        baiWanTest.setJieMoney(BigDecimal.valueOf(Double.parseDouble(lo.get(j).toString())));
                    }else if(j==4){
                        baiWanTest.setDaiKeMu(lo.get(j).toString());
                    }else if(j==5){
                        baiWanTest.setDaiMoney(BigDecimal.valueOf(Double.parseDouble(lo.get(j).toString())));
                    }else if(j==6){
                        baiWanTest.setJia(lo.get(j).toString());
                    }else if(j==7){
                        baiWanTest.setYi(lo.get(j).toString());
                    }else if(j==8){
                        baiWanTest.setSan(lo.get(j).toString());
                    }
                }
                if(EmptyUtils.isNotEmpty(baiWanTest)){
                    baiWanTest.setUserName("admin");
                    baiWanTestList.add(baiWanTest);
                }
            }

            MongoDbInfoVO mongoDbInfoVO = new MongoDbInfoVO();
            if (baiWanTestList.size() > 0) {
                Gson gson = new GsonBuilder()
                        .setDateFormat("yyyy-MM-dd HH:mm:ss")
                        .create();


                CryptoPrimitives suite = new CryptoPrimitives();
                suite.init();

                String attachmentHashValue=ImagePathHashValueUtil.getHashValue(attachment.toString(),suite);
                ImageVO imageVO=new ImageVO();
                imageVO.setAttachment(attachment.toString());
                imageVO.setHashValue(attachmentHashValue);

                mongoTemplate.insert(imageVO,"baiwanAttachmentTest");

                baiWanTestList.get(0).setAttachmentHashValue(attachmentHashValue);

                String jztLogisticsListStr = gson.toJson(baiWanTestList);

                String hashValue = ImagePathHashValueUtil.getHashValue(jztLogisticsListStr, suite);

                mongoDbInfoVO.setHashValue(hashValue);
                mongoDbInfoVO.setInfoList(baiWanTestList);
                mongoDbInfoVO.setInfoTableName("baiwan_test");

                String collName = "baiWanTestList";

                mongoTemplate.insert(mongoDbInfoVO,collName);
            }

            if (EmptyUtils.isNotEmpty(mongoDbInfoVO.getId())) {
                msg = "信息上链成功";
                return DtoUtil.returnSuccess(msg);
            } else  {
                msg = "信息上链失败";
                return DtoUtil.returnSuccess(msg);
            }

        }catch (Exception e){
            e.printStackTrace();
            msg="文件解析失败或者添加追溯信息错误";
        }


        return DtoUtil.returnSuccess(msg);
    }


    @RequestMapping(value = "/getBaiWanTestListPage", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public BackendPage<List<BaiWanTest>> queryPage(int page, int limit) {
        //构建参数
        Map<String, Object> param = new HashMap();
        try {
            BackendPage<List<BaiWanTest>> result = baiWanTestService.getBaiWanTestList(param, page, limit);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new BackendPage("查询异常");
        }
    }
}
